package com.company.netty.demo.NIO.ChannelDemos;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileChannelTest {

    public static void main(String[] args) throws Exception {
        /**
         * 使用ByteBuffer和Filechannel（通道），将“xxxx”写入到file.txt文件
         * */
        ByteBuffer buffer = ByteBuffer.allocate(20);

        String str = "xxxx张三";

        buffer.put(str.getBytes());

        //注意：必须要反转，否则读不到数据
        buffer.flip();

        //创建IO
        FileOutputStream fileOutputStream = new FileOutputStream("file.txt");

        //将stream转换成channel
        FileChannel channel = fileOutputStream.getChannel();

        //将buffer数据写入到channel
        channel.write(buffer);

        fileOutputStream.close();

    }

}
